package cn.jhc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import cn.jhc.bean.ShopBean;
import cn.jhc.bean.UserBean;

public class TitleDAO {

	private Connection con = null;

	public UserBean findUser(String username, String password) {
		String sql = "select id,name,password,displayname from user " + "where name=? "
				+ " and password=password(?)";
		PreparedStatement stat = null;
		ResultSet result = null;
		UserBean user = null;
		try {
			stat = con.prepareStatement(sql);
			stat.setString(1, username);
			stat.setString(2, password);
			result = stat.executeQuery();
			if (result.next()) {
				user = new UserBean();
				user.setId(result.getInt(1));
				user.setName(result.getString(2));
				user.setPassword(result.getString(3));
				user.setDisplayname(result.getString(4));
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				stat.close();

			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return user;
	}

	public UserBean findAdminUser(String username, String password) {
		String sql = "select id,name,password from adminuser "
				+ "where name=? " + " and password=password(?)";
		PreparedStatement stat = null;
		ResultSet result = null;
		UserBean user = null;
		try {
			stat = con.prepareStatement(sql);
			stat.setString(1, username);
			stat.setString(2, password);
			result = stat.executeQuery();
			if (result.next()) {
				user = new UserBean();
				user.setId(result.getInt(1));
				user.setName(result.getString(2));
				user.setPassword(result.getString(3));
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				stat.close();

			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return user;
	}

	
	public void addOrder(String name, String college, String classname,
			String chinesename, String email, String phone, String title,
			String amcount, String subtotal) {
		String sql = "insert into userOrder(name,college,classname,chinesename,email,phone,title,amcount,subtotal,date) "
				+ "values(?,?,?,?,?,?,?,?,?,now())";
		PreparedStatement st = null;
		try {
			con = ConnectionFactory.getConnection();
			st = con.prepareStatement(sql);
			st.setString(1, name);
			st.setString(2, college);
			st.setString(3, classname);
			st.setString(4, chinesename);
			st.setString(5, email);
			st.setString(6, phone);
			st.setString(7, title);
			st.setString(8, amcount);
			st.setString(9, subtotal);
			st.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public void setCon(Connection con) {
		this.con = con;
	}
}
